/*
 * File Name:               UIDragButton4_Hold.cs
 * 
 * Description:             普通类
 * Author:                  lisiyu <576603306@qq.com>

 * Create Date:             2017/06/03
 */

namespace UnityEngine.UI
{
    using UnityEngine;
    using EventSystems;
    using System;

    [AddComponentMenu("UI/DragButton - 4个事件 - 按住")]
    [DisallowMultipleComponent]
    [RequireComponent(typeof(Image))]
    public class UIDragButton4_Hold : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    {
        public Action<ModeType, bool> onClick;

        public Sprite Sprite_Center;
        public Sprite Sprite_Up;
        public Sprite Sprite_Down;
        public Sprite Sprite_Left;
        public Sprite Sprite_Right;

        public float MaxDistance = 15f;             // 移动节点与固定节点的最大距离
        private Vector3 mPointerDownPos;            // 按下时的位置

        private Vector3 mInitPos;
        private Image mImage;
        void Awake()
        {
            mInitPos = transform.localPosition;
            mImage = GetComponent<Image>();
        }

        public void OnDrag(PointerEventData eventData)
        {
            setMovedPos_ByScreen(eventData.position);

            onClick(mModeType, true);
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            mPointerDownPos = UIManager.Singleton.ScreenToWorldPoint(eventData.position, transform.position.z);
            setMovedPos_ByScreen(eventData.position);

            onClick(mModeType, true);
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            mImage.sprite = Sprite_Center;
            transform.localPosition = mInitPos;

            onClick(mModeType, false);
        }

        public enum ModeType
        {
            Center,
            Up,
            Down,
            Left,
            Right,
        }

        private ModeType mModeType;
        private void setMovedPos_ByScreen(Vector2 screenPos)
        {
            var retPos = UIManager.Singleton.ScreenToWorldPoint(screenPos, transform.position.z);

            var dir = (retPos - mPointerDownPos);
            if (dir.magnitude > MaxDistance)
            {
                dir = (retPos - mPointerDownPos).normalized * MaxDistance;
            }

            if (dir.magnitude > MaxDistance / 2)
            {
                if (dir.y > MaxDistance / 3)
                {
                    mModeType = ModeType.Up;
                    mImage.sprite = Sprite_Up;
                }
                else if (dir.y < MaxDistance / -3)
                {
                    mModeType = ModeType.Down;
                    mImage.sprite = Sprite_Down;
                }
                else if (dir.x < MaxDistance / -3)
                {
                    mModeType = ModeType.Left;
                    mImage.sprite = Sprite_Left;
                }
                else
                {
                    mModeType = ModeType.Right;
                    mImage.sprite = Sprite_Right;
                }
            }
            else
            {
                mModeType = ModeType.Center;
                mImage.sprite = Sprite_Center;
            }

            retPos = mPointerDownPos + dir;
            transform.position = retPos;
        }

        private void OnDisable()
        {
            mImage.sprite = Sprite_Center;
            transform.localPosition = mInitPos;
        }
    }
}